import { updateOrder } from '@/api/order'
import { backTo } from '@/utils/unifunc'

const makCall = async (phone: string) => {
  await uni.makePhoneCall({
    phoneNumber: phone
  })
}

const reportOrder = async (id: number, status: number) => {
  const res = await updateOrder<any, RepairOrder>({
    id,
    status: status + 1
  })
  if (res.code === 200) {
    backTo()
  }
}

export function getUserButtons(status: number): Button[] {
  switch (status) {
    case 0:
      // 不用this，直接使用箭头函数，如果要this就得使用function关键字
      return [
        {
          label: '支付',
          action: 'report',
          onPress: reportOrder
        }
      ]
    case 1:
      return [
        {
          label: '联系客服',
          action: 'contact',
          onPress: makCall
        }
      ]
    case 2:
      return [
        {
          label: '联系客服',
          action: 'contact',
          onPress: makCall
        }
      ]
    case 3:
      return [
        {
          label: '联系师傅',
          action: 'contact',
          onPress: makCall
        }
      ]
    case 4:
      return [
        {
          label: '举报',
          action: 'report',
          onPress: makCall
        }
      ]
    case 5:
    default:
      return []
  }
}

export function getMasterButtons(status: number): Button[] {
  switch (status) {
    case 2:
      return [
        {
          label: '联系用户',
          action: 'contact',
          onPress: makCall
        },
        {
          label: '确认接单',
          action: 'report',
          onPress: reportOrder
        }
      ]
    case 3:
      return [
        {
          label: '联系用户',
          action: 'report',
          onPress: makCall
        },
        {
          label: '我已到场',
          action: 'report',
          onPress: reportOrder
        }
      ]
    case 4:
      return [
        {
          label: '我已完成',
          action: 'report',
          onPress: reportOrder
        }
      ]
    case 5:
    default:
      return []
  }
}

export function getAdminButtons(status: number): Button[] {
  switch (status) {
    case 0:
      return [
        {
          label: '更改状态',
          action: 'report',
          onPress: reportOrder
        }
      ]
    case 1:
      return [
        {
          label: '指派给师傅',
          action: 'dispatch',
          onPress: async (id: number, master_id: number, status: number) => {
            const res = await updateOrder<any, RepairOrder>({
              id,
              tech_id: master_id
            })
            if (res.code === 200) {
              await reportOrder(id, status)
              backTo()
            }
          }
        }
      ]
    case 2:
      return [
        {
          label: '更改状态',
          action: 'report',
          onPress: reportOrder
        }
      ]
    case 3:
      return [
        {
          label: '更改状态',
          action: 'report',
          onPress: reportOrder
        }
      ]
    case 4:
      return [
        {
          label: '更改状态',
          action: 'report',
          onPress: reportOrder
        }
      ]
    case 5:
    default:
      return []
  }
}
